import 'package:flutter/material.dart';
import 'package:uikit/utils/screen_util/size_extension.dart';

class LoginInput extends StatefulWidget {
  const LoginInput(
    this.title,
    this.hint, {
    Key? key,
    this.onChanged,
    this.focusChanged,
    this.lineStretch = false,
    this.obscureText = false,
    this.keyboardType,
  }) : super(key: key);

  final String title;
  final String hint;
  final ValueChanged<String>? onChanged;
  final ValueChanged<bool>? focusChanged;
  final bool lineStretch;
  final bool obscureText;
  final TextInputType? keyboardType;

  @override
  _LoginInputState createState() => _LoginInputState();
}

class _LoginInputState extends State<LoginInput> {
  final _focusNode = FocusNode();

  static const MaterialColor primary = MaterialColor(
    0xfffb7299,
    <int, Color>{
      50: Color(0xffff9db5),
    },
  );

  @override
  void initState() {
    super.initState();
    //是否获取光标的监听
    _focusNode.addListener(() {
      print("hasFocus = ${_focusNode.hasFocus}");
      if (widget.focusChanged != null) {
        widget.focusChanged!(_focusNode.hasFocus);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildLeftTextWidget(),
            _buildInputWidget(),
          ],
        ),
        _buildDivider(),
      ],
    );
  }

  /// 左边文字
  Widget _buildLeftTextWidget() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 15.w),
      width: 100.w,
      child: Text(
        widget.title,
        style: TextStyle(fontSize: 16.sp),
      ),
    );
  }

  /// 输入框
  Widget _buildInputWidget() {
    return Expanded(
        child: TextField(
      focusNode: _focusNode,
      onChanged: widget.onChanged,
      obscureText: widget.obscureText,
      keyboardType: widget.keyboardType,
      autofocus: !widget.obscureText,
      cursorColor: primary,
      style: TextStyle(fontSize: 16.sp, color: Colors.black, fontWeight: FontWeight.w300),
      //输入框的样式
      decoration: InputDecoration(
          contentPadding: EdgeInsets.only(left: 20.w, right: 20.w),
          border: InputBorder.none,
          hintText: widget.hint,
          hintStyle: TextStyle(fontSize: 15.sp, color: Colors.grey)),
    ));
  }

  /// 分割线
  Widget _buildDivider() {
    return Padding(
      padding: EdgeInsets.only(left: !widget.lineStretch ? 15 : 0),
      child: Divider(
        height: 1.w,
        thickness: 0.5,
      ),
    );
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }
}
